package bigraph.model;


import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.views.properties.IPropertySource;

public class Node implements IAdaptable {

	private String name;
	private Rectangle layout = null; //TODO Switch to more general polygon to allow inheriting different shapes
	private List<Node> children;
	private Node parent;
	private PropertyChangeSupport listeners;
	public static final String PROPERTY_LAYOUT = "NodeLayout";
	public static final String PROPERTY_ADD = "NodeAddChild";
	public static final String PROPERTY_REMOVE = "NodeRemoveChild";
	public static final String PROPERTY_RENAME = "NodeRename";
	private IPropertySource propertySource = null;
	
	public Node() {
		  this.name = "Unknown";
		  this.children = new ArrayList<Node>();
		  this.layout = new Rectangle(10,10,100,100);
		  this.parent = null;
		  this.listeners = new PropertyChangeSupport(this);
	}


    public void setName(String name) {
          String oldName = this.name;
    	  this.name = name;
          getListeners().firePropertyChange(PROPERTY_RENAME, oldName, this.name);
    }
    
    public String getName() {
          return this.name;
    }
    
    public void setLayout(Rectangle newLayout) {
    	  Rectangle oldLayout = this.layout;
    	  this.layout = newLayout;
    	  getListeners().firePropertyChange(PROPERTY_LAYOUT, oldLayout, newLayout);
    }

    public Rectangle getLayout() {
          return this.layout;
    }

    public List<Node> getChildrenArray() {
          return this.children;
    }
    
    public void setParent(Node parent) {
          this.parent = parent;
    }
   
    public Node getParent() {
          return this.parent;
    }
    
    public boolean contains(Node child) {
          return children.contains(child);
    }
    
	public boolean addChild(Node child) {
		  boolean b = this.children.add(child);
		  if(b) {
			  child.setParent(this);
			  getListeners().firePropertyChange(PROPERTY_ADD, null, child);
		  }
		  return b;
	}

	public boolean removeChild(Node child) {
		  boolean b = this.children.remove(child);
		  if(b) {
			  getListeners().firePropertyChange(PROPERTY_REMOVE, child, null);
		  }
		  return b;
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
	     listeners.addPropertyChangeListener(listener);
	}
	
	public PropertyChangeSupport getListeners() {
		  return listeners;
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener) {
	      listeners.removePropertyChangeListener(listener);
	}
	
	@Override
	public Object getAdapter(Class adapter) {
	     if (adapter == IPropertySource.class) {
	           if (propertySource == null)
	                  propertySource = new NodePropertySource(this);
	           return propertySource;
	     }
	     return null;
	}



}

